package net.lab1024.sa.admin.module.business.school.floor.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import net.lab1024.sa.admin.module.business.school.floor.dao.FloorDao;
import net.lab1024.sa.admin.module.business.school.floor.domain.entity.FloorEntity;
import net.lab1024.sa.admin.module.business.school.floor.domain.form.FloorAddForm;
import net.lab1024.sa.admin.module.business.school.floor.domain.form.FloorQueryForm;
import net.lab1024.sa.admin.module.business.school.floor.domain.form.FloorUpdateForm;
import net.lab1024.sa.admin.module.business.school.floor.domain.vo.FloorVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.math3.analysis.function.Floor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 楼层 Service
 *
 * @Author ZouYang
 * @Date 2024-12-28 12:10:44
 * @Copyright ZouYang
 */

@Service
public class FloorService {

    @Resource
    private FloorDao dao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<FloorVO> queryPage(FloorQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<FloorVO> list = dao.queryPage(page, queryForm);
        PageResult<FloorVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(FloorAddForm addForm) {
        FloorEntity floorEntity = SmartBeanUtil.copy(addForm, FloorEntity.class);
        dao.insert(floorEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(FloorUpdateForm updateForm) {
        FloorEntity floorEntity = SmartBeanUtil.copy(updateForm, FloorEntity.class);
        dao.updateById(floorEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        dao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        dao.updateDeleted(id, true);
        return ResponseDTO.ok();
    }

    public List<FloorEntity> initFloorByDormitory(Long dormitoryId){
        // 已存在的”层“
        List<FloorEntity> floors = dao.selectByDormitoryId(dormitoryId);
        List<Integer> floorNames = floors.stream().map(FloorEntity::getName).collect(Collectors.toList());

        // 构建待创建的”层“
        List<FloorEntity> floorList = new ArrayList<>(6);
        for (int i = 0; i < 6; i++) {
            if(floorNames.contains(i)){
                continue;
            }

            FloorEntity floor = new FloorEntity();
            floor.setDormitoryId(dormitoryId);
            floor.setName(i);
        }
        dao.batchInsert(floorList);
        return floorList;
    }
}
